---
title: "Vignette experiment"
author: "Endre Borbáth, Swen Hutter"
date: "20.07.2025"
output:
  html_document:
    toc: no
---

```{r, echo=F, message=F, error=F, warning=FALSE}

# setting up R

rm(list = ls())

library(haven)
library(tidyverse)
library(stargazer)
library(sjPlot)
library(dotwhisker)
library(texreg)
library(lmtest)
library(sandwich)
library(estimatr)
library(here)

```


```{r, echo=F, message=F, error=F, warning=FALSE}

control_vars <- c("age", "time", "group_in_need", 
                  "emo_convergence", "edu_level", 
                  "income", "region", "gndr", "age2")
factors <- c("time", "org", "group_in_need", 
             "emo_convergence", "edu_level", "income", "region", "gndr")
numeric <- c("age", "age2")

org_lvls <- c("Friends and Acquaintances","Local Civil Society Organization", "National Civil Society Organization", "Municipal Administration")


load(here("study3_experiment.RData"))

```

# Note for the analysis as a whole:

- All the analysis is based on 'long format' - every respondent was asked twice about the experiment and the two are treated as independent observations, doubling the sample size. However, all regressions include clustered standard errors by respondent ID.

# Regressions 

```{r, results='asis', include = T, echo = F, message = F, warning=FALSE}

M1 <- lm_robust(volunteering_DV ~ org + group_in_need + emo_convergence + time + cntry,
                data = orig_dat, 
                clusters=id, se_type = "stata")

htmlreg(l=list(M1), doctype = FALSE, center = TRUE, siunitx=TRUE, 
       use.packages=FALSE, booktabs = TRUE, float.pos = "h!",
       caption = "Mobilizing during the COVID-19 crisis",
       single.row=TRUE, no.margin = TRUE, include.ci = FALSE,
       custom.model.names = c("Volunteering"),
       custom.coef.map = list(#"(Intercept)" = "Intercept",
                              "org2" = "Local Organization (ref: Friends)", 
                              "org3" = "National Organization",
                              "org4" = "Municipal Administration",
                              "group_in_need2" = "People with financial diff. (ref: elderly)",
                              "emo_convergence2" = "Emotional convergence",
                              "time2" = "Asked second time (ref: first)",
                              "cntryIT" = "Italy",
                              "cntryPL" = "Poland"),
       groups = list("Mobilizing Agent"=1:3,
                     "Other treatments"=4:5,
                     "Country FE (ref: Germany)"=7:8),
       include.deviance = FALSE,
       include.loglik = FALSE,
       # file=paste0(fig_path, "general_participation.tex"),
      custom.note	= "%stars")

```


```{r, include = F, echo = F, message = F, warning=FALSE}

# Calculate the predicted probabilities

new_dat <- data.frame("org" = as.factor(seq(1, 4, 1)),
                      "group_in_need" = as.factor(1),
                      "emo_convergence" = as.factor(1),
                      "time" = as.factor(1),
                      "cntry" = as.factor("DE"))
                      

probs = predict(M1, newdata = new_dat, type = "response", se.fit = TRUE)

probs <- data.frame(
           org = as.factor(seq(1, 4, 1)),
           pm = probs$fit,
           pu = probs$fit + probs$se.fit * 1.96, # 95% confidence interval
           pl = probs$fit - probs$se.fit * 1.96) # 95% confidence interval

```

## Predicted values for volunteering

```{r, include = T, echo = F, message = F, warning=FALSE, fig.width=6, fig.height=4, fig.align="center"}

ggplot(probs, aes(x=org, y=pm, group=1)) +
  geom_point() + 
  geom_line(linetype="dashed") +
  geom_errorbar(aes(ymin=pl, ymax=pu), width=.2,
                 position=position_dodge(0.05)) +
  scale_x_discrete(labels=stringr::str_wrap(org_lvls, 22)) + 
  # ylim(0, 10) +
  theme_linedraw() +
  ylab("Effect of mobilizing agent") + 
  # xlab("being contacted by an organization")
  theme(axis.title.x=element_blank())

```

